#include "hublnk.h"
#include "ui_hublnk.h"

#include <QDir>
#include <QFileIconProvider>
#include <QMouseEvent>
#include <QToolTip>
#include "settinghandler.h"

#define HUB_LINKS_DIR QString("./hub_links/")

HubLNK::HubLNK(QWidget *parent) :
    QPushButton(parent),
    ui(new Ui::HubLNK)
{
    ui->setupUi(this);

    setIcon(QIcon(":/images/hub.png"));
    setFlat(true);
}

void HubLNK::set_size(const QSize &size)
{
    resize(size);
    setIconSize(size);
}

void HubLNK::mousePressEvent(QMouseEvent *)
{
    // 变大
    set_size(QSize(SettingHandler::get_instance()->get_widget_height(), SettingHandler::get_instance()->get_widget_height()));
    move(old_x - SettingHandler::get_instance()->get_border_size(), 0);

    this_is_clicked = true;
}

void HubLNK::mouseReleaseEvent(QMouseEvent *event)
{
    // 还原
    set_size(QSize(SettingHandler::get_instance()->get_lnk_size(), SettingHandler::get_instance()->get_lnk_size()));
    move(old_x, old_y);

    if (this_is_clicked)
    {
        auto what_button = event->button();
        if (what_button == Qt::LeftButton)
            emit left_clicked();
        if (what_button == Qt::RightButton)
            emit right_clicked();
    }
}

void HubLNK::mouseMoveEvent(QMouseEvent *)
{
    this_is_clicked = false;
}

void HubLNK::enterEvent(QEnterEvent *)
{
    old_x = x();
    old_y = y();
    QToolTip::showText(mapToGlobal(QPoint(0, -50)), "更多", this);
    // 变大
    set_size(QSize(SettingHandler::get_instance()->get_widget_height(), SettingHandler::get_instance()->get_widget_height()));
    move(old_x - SettingHandler::get_instance()->get_border_size(), 0);
}

void HubLNK::leaveEvent(QEvent *)
{
    // 还原
    set_size(QSize(SettingHandler::get_instance()->get_lnk_size(), SettingHandler::get_instance()->get_lnk_size()));
    move(old_x, old_y);
}

HubLNK::~HubLNK()
{
    qDebug() << "HubLNK 销毁了";

    delete ui;
}
